package com.wl.mvi

import android.app.Application
import android.util.Log
import com.alibaba.android.arouter.launcher.ARouter
import com.wl.lib.http.DomainManager
import com.wl.lib.utils.ApplicationHelper
import com.wl.lib.utils.FileDownloader
import com.wl.lib.utils.FileUtils
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

// Created by wangliang on 2024/4/25.
class App : Application() {

    override fun onCreate() {
        super.onCreate()
        ApplicationHelper.init(this)

        initARouter()

        DomainManager.mainDomain = "https://www.wanandroid.com"

        loadLocalWebRes()
    }

    private fun initARouter() {
        //TODO Debug 模式判断，release 需要删除
        ARouter.openLog()
        ARouter.openDebug()

        ARouter.init(this)

        val space = FileUtils.getExternalFileAvailableSpace(this)
        Log.d("WLTest", "space ${FileUtils.formatSize(space)}")
    }

    private fun loadLocalWebRes() {
        val url = "https://ruixueyun.oss-cn-beijing.aliyuncs.com/ruixue/website/landing/dev-error_2aab66bf-0975-4cec-8cd8-92651381454b.zip"

        val downloads = listOf(url)
        GlobalScope.launch {
            FileDownloader.downloadFiles(downloads, rootPath = FileUtils.getExternalFileWebRootPath(this@App),
                onProgress = { url, progress ->
                    Log.d("WLTest", "下载进度: $url -> ${String.format("%.2f", progress * 100)}%")
                }, onFinished = {}, needUnzip = true)
        }
    }
}